﻿using System.Windows.Markup;
using System.Windows;
using System.Globalization;

namespace Invini.Localization
{
    [ContentProperty("Content")]
    public class Localization
    {
        public static event LocalizationContextChangedEventHandler ContextChanged;

        public static readonly DependencyProperty ContextProperty = DependencyProperty.RegisterAttached(
          "Context",
          typeof(object),
          typeof(Localization),
          new FrameworkPropertyMetadata(default(object), FrameworkPropertyMetadataOptions.AffectsRender, contextChanged)
        );

        public static object GetContext(DependencyObject element)
        {
            return element.GetValue(ContextProperty);
        }
        public static void SetContext(DependencyObject element, object value)
        {
            element.SetValue(ContextProperty, value);
        }

        public CultureInfo Culture
        {
            get;
            set;
        }
        public object Content
        {
            get;
            set;
        }

        public Localization()
        {
            this.Culture = CultureInfo.InvariantCulture;
        }

        private static void contextChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
        {
            onContextChanged(new LocalizationContextChangedEventArgs(dp, e.NewValue));
        }

        private static void onContextChanged(LocalizationContextChangedEventArgs e)
        {
            if (ContextChanged != null)
                ContextChanged(null, e);
        }
    }
}
